home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / comm / tcp / mufs_telnetd.lha / telnetd2_0.lha / telnetd-2.0 / source / RCS / TnServ.c < prev   
C/C++ Source or Header  |  1995-04-13  |  9KB  |  357 lines

  1. head    2.0;
  2. access;
  3. symbols;
  4. locks; strict;
  5. comment    @ * @;
  6.  
  7.  
  8. 2.0
  9. date    95.04.13.19.53.48;    author simons;    state Exp;
  10. branches;
  11. next    1.5;
  12.  
  13. 1.5
  14. date    95.01.24.02.42.20;    author simons;    state Exp;
  15. branches;
  16. next    1.4;
  17.  
  18. 1.4
  19. date    95.01.22.19.37.23;    author simons;    state Exp;
  20. branches;
  21. next    1.3;
  22.  
  23. 1.3
  24. date    95.01.22.19.30.16;    author simons;    state Exp;
  25. branches;
  26. next    1.2;
  27.  
  28. 1.2
  29. date    95.01.22.18.48.36;    author simons;    state Exp;
  30. branches;
  31. next    1.1;
  32.  
  33. 1.1
  34. date    95.01.22.18.07.20;    author simons;    state Exp;
  35. branches;
  36. next    ;
  37.  
  38.  
  39. desc
  40. @TnServ sits in the background and fetches incoming telnet
  41. connects, handing them over to TelnetGetty.
  42. @
  43.  
  44.  
  45. 2.0
  46. log
  47. @Updated my e-mail address to .rhein.de.
  48. @
  49. text
  50. @/*
  51.  *      $Filename: TnServ.c $
  52.  *      $Revision: 1.5 $
  53.  *      $Date: 1995/01/24 02:42:20 $
  54.  *
  55.  *      Copyright (C) 1993,94 by Steve Holland <sdh4@@cornell.edu>
  56.  *      Copyright (C) 1995 by Peter Simons <simons@@peti.rhein.de>
  57.  *
  58.  *      This program is free software; you can redistribute it and/or
  59.  *      modify it under the terms of the GNU General Public License as
  60.  *      published by the Free Software Foundation; either version 2 of
  61.  *      the License, or (at your option) any later version.
  62.  *
  63.  *      This program is distributed in the hope that it will be useful,
  64.  *      but WITHOUT ANY WARRANTY; without even the implied warranty of
  65.  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  66.  *      General Public License for more details.
  67.  *
  68.  *      You should have received a copy of the GNU General Public License
  69.  *      along with this program; if not, write to the Free Software
  70.  *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  71.  *
  72.  *      $Id: TnServ.c 1.5 1995/01/24 02:42:20 simons Exp simons $
  73.  *
  74.  */
  75.  
  76. /************************************* includes ***********/
  77. #include <sys/types.h>
  78. #include <sys/socket.h>
  79. #include <netinet/in.h>
  80. #include <proto/socket.h>
  81. #include <netdb.h>
  82. #include <errno.h>
  83. #include <dos/dos.h>
  84. #include <dos/dostags.h>
  85. #include <proto/dos.h>
  86. #include <proto/exec.h>
  87. #include <clib/netlib_protos.h>
  88.  
  89. #include <stdio.h>
  90. #include <string.h>
  91. #include <stdlib.h>
  92.  
  93. #include "TnServ_rev.h"
  94.  
  95. /************************************* defines ************/
  96.  
  97. /************************************* global variables ***/
  98. static const char __DOSVer[] = VERSTAG " written by Peter Simons <simons@@peti.rhein.de>";
  99.  
  100. int main(void)
  101. {
  102.         struct servent *sp;
  103.         struct sockaddr_in ServAddr;
  104.         LONG Socket, SubSock, SubSockID;
  105.         int WaitSig;
  106.         static char ArgStr[200];
  107.         BPTR TelNetProg;
  108.         long Len;
  109.  
  110.         if (!(sp = getservbyname("telnet", "tcp")))
  111.                 return 21;
  112.  
  113.         bzero(&ServAddr, sizeof(ServAddr));
  114.         ServAddr.sin_port = sp->s_port;
  115.         ServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  116.         ServAddr.sin_family = AF_INET;
  117.         ServAddr.sin_len = sizeof(ServAddr);
  118.         Socket = socket(AF_INET, SOCK_STREAM, 0);
  119.         if (Socket < 0)
  120.                 return 22;
  121.         bind(Socket, (struct sockaddr *) &ServAddr, sizeof(ServAddr));
  122.         listen(Socket, 5);
  123.         WaitSig = AllocSignal(-1);
  124.         if (WaitSig == -1)
  125.                 return 23;
  126.  
  127.         for (;;) {
  128.                 Len = sizeof(ServAddr);
  129.                 SubSock = accept(Socket, (struct sockaddr *) &ServAddr, &Len);
  130.                 if (SubSock < 0) {
  131.                         if (Errno() == EINTR) {
  132.                                 CloseSocket(Socket);
  133.                                 return 0;
  134.                         }
  135.                         continue;
  136.                 }
  137.                 SetSignal(0, (1 << WaitSig));   /* unset WaitSig signal */
  138.                 SubSockID = ReleaseSocket(SubSock, UNIQUE_ID);
  139.                 sprintf(ArgStr, "%u %u %u", (unsigned long) SubSockID, (unsigned long) WaitSig, (unsigned long) FindTask(NULL));
  140.                 if (TelNetProg = LoadSeg("AmiTCP:serv/telnetd")) {
  141.                         if (CreateNewProcTags(NP_Seglist, TelNetProg,
  142.                                                 NP_Name, "telnet login",
  143.                                                 NP_Arguments, ArgStr,
  144.                                                 NP_StackSize, 12000,
  145.                                                 NP_FreeSeglist, TRUE,
  146.                                                 TAG_END)) {
  147.  
  148.                                 /* CreateProc("telnet login",0,TelNetProg,12000); */
  149.                                 Wait(1 << WaitSig);     /* Wait for new proc to start up */
  150.                         }
  151.                 }
  152.         }
  153.  
  154. }
  155.  
  156.  
  157. /************************************* subroutines ********/
  158. @
  159.  
  160.  
  161. 1.5
  162. log
  163. @TnServ expects the daemon now as AmiTCP:serv/telnetd. The tnserv:-
  164. assign is history.
  165. @
  166. text
  167. @d3 2
  168. a4 2
  169.  *      $Revision: 1.4 $
  170.  *      $Date: 1995/01/22 19:37:23 $
  171. d7 1
  172. a7 1
  173.  *      Copyright (C) 1995 by Peter Simons <simons@@peti.GUN.de>
  174. d23 1
  175. a23 1
  176.  *      $Id: TnServ.c 1.4 1995/01/22 19:37:23 simons Exp simons $
  177. d49 1
  178. a49 1
  179. static const char __DOSVer[] = VERSTAG " written by Peter Simons <simons@@peti.GUN.de>";
  180. @
  181.  
  182.  
  183. 1.4
  184. log
  185. @Now using the autoinit functions in net.lib, rather than installing
  186. an atexit() hook myself.
  187. @
  188. text
  189. @d3 2
  190. a4 2
  191.  *      $Revision: 1.3 $
  192.  *      $Date: 1995/01/22 19:30:16 $
  193. d23 1
  194. a23 1
  195.  *      $Id: TnServ.c 1.3 1995/01/22 19:30:16 simons Exp simons $
  196. d91 1
  197. a91 8
  198.                 TelNetProg = LoadSeg("tnserv:TelnetGetty");
  199.                 if (!TelNetProg)
  200.                         TelNetProg = LoadSeg("TelnetGetty/TelnetGetty");
  201.                 if (!TelNetProg)
  202.                         TelNetProg = LoadSeg("TelnetGetty");
  203.                 if (!TelNetProg)
  204.                         TelNetProg = LoadSeg("AmiTCP:Bin/TelnetGetty");
  205.                 if (TelNetProg) {
  206. @
  207.  
  208.  
  209. 1.3
  210. log
  211. @Added version string and "written by"-line with my name.
  212. @
  213. text
  214. @d3 2
  215. a4 2
  216.  *      $Revision: 1.2 $
  217.  *      $Date: 1995/01/22 18:48:36 $
  218. d23 1
  219. a23 1
  220.  *      $Id: TnServ.c 1.2 1995/01/22 18:48:36 simons Exp simons $
  221. d38 1
  222. a46 1
  223. void FreeResources(void);
  224. a49 1
  225. struct Library *SocketBase = NULL;
  226. a60 6
  227.         if (atexit(FreeResources))
  228.                 return 20;
  229.  
  230.         if (!(SocketBase = OpenLibrary("bsdsocket.library", 2)))
  231.                 return 20;
  232.  
  233. a115 7
  234. void FreeResources(void)
  235. {
  236.         if (SocketBase)
  237.                 CloseLibrary(SocketBase);
  238. }
  239.  
  240.  
  241. @
  242.  
  243.  
  244. 1.2
  245. log
  246. @Changed source to compile under SAS/C 6.51.
  247. Added GNU GPL header and my copyright.
  248. Reformatted source with indent.
  249. Added atexit() hook to close the library when exiting.
  250. @
  251. text
  252. @d3 2
  253. a4 2
  254.  *      $Revision$
  255.  *      $Date$
  256. d23 1
  257. a23 1
  258.  *      $Id$
  259. d49 1
  260. a49 1
  261. static const char __DOSVer[] = VERSTAG;
  262. @
  263.  
  264.  
  265. 1.1
  266. log
  267. @Initial revision
  268. @
  269. text
  270. @d1 27
  271. d38 1
  272. d43 8
  273. a50 3
  274. struct Library *SocketBase=NULL;
  275. #define bzero(base,n) memset(base,0,n)
  276. #define bcopy(from,to,n) memcpy(to,from,n)
  277. d52 1
  278. a52 1
  279. void Quit(int retcode)
  280. d54 65
  281. a118 3
  282.     
  283.     if (SocketBase) CloseLibrary(SocketBase);
  284.     exit(retcode);
  285. d121 3
  286. a123 1
  287. struct Process *MyCreateNewProcTags(ULONG Tag0,...)
  288. d125 2
  289. a126 1
  290.     return CreateNewProc((struct TagItem *)&Tag0);
  291. d129 1
  292. a129 63
  293. void main(void)
  294. {
  295.     struct servent *sp;
  296.     struct sockaddr_in ServAddr;
  297.     int Socket,SubSock;
  298.     LONG SubSockID;
  299.     int WaitSig;
  300.     static char ArgStr[200];
  301.     BPTR TelNetProg;
  302.     long Len;
  303.     
  304.     SocketBase=OpenLibrary("bsdsocket.library",2);
  305.     
  306.     if (!SocketBase) Quit(20);
  307.     
  308.     sp=getservbyname("telnet","tcp");
  309.     if (!sp) Quit(21);
  310.     
  311.     bzero(&ServAddr,sizeof(ServAddr));
  312.     ServAddr.sin_port=sp->s_port;
  313.     ServAddr.sin_addr.s_addr=htonl(INADDR_ANY);
  314.     ServAddr.sin_family=AF_INET;
  315.     ServAddr.sin_len=sizeof(ServAddr);
  316.     Socket=socket(AF_INET,SOCK_STREAM,0);
  317.     if (Socket < 0) Quit(22);
  318.     bind(Socket,&ServAddr,sizeof(ServAddr));
  319.     listen(Socket,5);
  320.     WaitSig=AllocSignal(-1);
  321.     if (WaitSig==-1) Quit(23);
  322.     
  323.     
  324.     for (;;) {
  325.         Len=sizeof(ServAddr);
  326.         SubSock=accept(Socket,&ServAddr,&Len);
  327.         if (SubSock < 0) {
  328.             if (Errno()==EINTR) {
  329.                 CloseSocket(Socket);
  330.                 Quit(0);
  331.             }
  332.             continue;
  333.         }
  334.         SetSignal(0,(1<<WaitSig)); /* unset WaitSig signal */
  335.         SubSockID=ReleaseSocket(SubSock,UNIQUE_ID);
  336.         sprintf(ArgStr,"%u %u %u",(unsigned long)SubSockID,(unsigned long)WaitSig,(unsigned long)FindTask(NULL));
  337.         TelNetProg=LoadSeg("tnserv:TelnetGetty");
  338.         if (!TelNetProg) TelNetProg=LoadSeg("TelnetGetty/TelnetGetty");
  339.         if (!TelNetProg) TelNetProg=LoadSeg("TelnetGetty");
  340.         if (!TelNetProg) TelNetProg=LoadSeg("AmiTCP:Bin/TelnetGetty");
  341.         if (TelNetProg) {
  342.             if (MyCreateNewProcTags(NP_Seglist,TelNetProg,
  343.                 NP_Name,"telnet login",
  344.                 NP_Arguments,ArgStr,
  345.                 NP_StackSize,12000,
  346.                 NP_FreeSeglist,TRUE,
  347.                 TAG_END)) {
  348.                 
  349.                 /*CreateProc("telnet login",0,TelNetProg,12000);*/
  350.                 Wait(1<<WaitSig); /* Wait for new proc to start up */
  351.             }
  352.         }
  353.     }
  354.     
  355. }
  356. @
  357.